<?php
// full path to kb directory, if it does no work write it manually
$app_dir = str_replace(array('\\'), '/', getcwd() . '/');
//$app_dir = $_SERVER['DOCUMENT_ROOT'] . '/kb/';


/* DO NOT MODIFY */
$admin_dir  = $app_dir . 'admin/';
$client_dir = $app_dir . 'client/inc/';
$cron_dir   = $app_dir . 'client/cron/';
$filename   = $app_dir . 'kb.rss';
$category_id = (!empty($_GET['c'])) ? intval(($_GET['c'])) : false;


require_once $admin_dir . 'config.inc.php';
require_once $client_dir . 'common.inc.php';
require_once 'eleontev/XML/RSSCreator.php';
require_once 'eleontev/Util/ResultCache.php';

require_once $client_dir . 'KBClientController.php';
require_once $client_dir . 'KBClientRSSModel.php';
require_once $client_dir . 'DocumentParser.php';


$manager = &new KBClientRSSModel();
$categories = &$manager->getCategories();
$settings = &$manager->getSettings();

if($settings['rss_generate'] == 'none') {
	exit;

} elseif($settings['rss_generate'] == 'one') {
	$category_id = false;

} elseif($settings['rss_generate'] == 'top') {
	if($category_id && (!isset($categories[$category_id]) || 
						$categories[$category_id]['parent_id'] != 0)) {
		exit;
	}
}


$controller = &new KBClientController();
$controller->setDirVars($settings);
$controller->setModRewrite(false);

$channel_data = &$manager->getChannelData($category_id, $categories, $controller);
$entries = &$manager->getEntries($category_id, $categories, $controller, 15, 1020);

$rss = &new RSSCreator();
$rss->setEncoding($conf['lang']['meta_charset']);
$rss->setChannel('title', $channel_data['title']);
$rss->setChannel('link', $channel_data['link']);
$rss->setChannel('description', $channel_data['description']);	

foreach($entries as $k => $v) {
	$rss->setItem($v['title'], $v['link'], $v);
}

//echo "<pre>"; print_r($filename); echo "</pre>";
//FileUtil::write($filename, $rss->getXML());

echo $rss->getXML(true);
?>